package com.ruoyi.flowable.listener.asset;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.equ.domain.EquStockin;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

import static com.ruoyi.flowable.listener.asset.AssetListener_3.getApprovalRecord;

/**
 * 验收-只记录审批信息 5.1-5.4
 */
@Slf4j
@Component("assetCommonListener")
public class AssetCommonListener implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            EquStockin equStockin = execution.getVariable("equStockin", EquStockin.class);
            //审批记录
            List<Map<String, String>> approvalRecordList = getApprovalRecord(equStockin.getApprovalRecord());
            String approvalRecord = objectMapper.writeValueAsString(approvalRecordList);

            //审批信息
            String approvalInfo = equStockin.getApprovalInfo();
            Map<String, Object> approvalInfoMap = objectMapper.readValue(
                    approvalInfo,
                    new TypeReference<Map<String, Object>>() {});
            approvalInfoMap.put("审批记录", approvalRecordList);
            approvalInfo = objectMapper.writeValueAsString(approvalInfoMap);

            //赋值
            equStockin.setApprovalRecord(approvalRecord);

            equStockin.setApprovalInfo(approvalInfo);

            execution.setVariable("equStockin", equStockin);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
